package com.changgou.search.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * rabbitMQ配置类
 */
@Configuration
public class RabbitMQConfig {
  //1定义队列名称
  public static final String AD_UPDATE_QUEUE = "ad_update_queue";

  //2定义交换机  GOODS_UP_EXCHANGE和队列SEARCH_ADD_QUEUE
  private static final String GOODS_UP_EXCHANGE = "goods_up_exchange";
  //2定义队列名称
  private static final String SEARCH_ADD_QUEUE = "search_add_queue";


  //1声明队列
  @Bean
  public Queue queue() {
    return new Queue(AD_UPDATE_QUEUE); //声明的队列名
  }

  //2声明队列
  @Bean(SEARCH_ADD_QUEUE)  //给队列这个Bean设置个名称
  public Queue SEARCH_ADD_QUEUE() {
    return new Queue(SEARCH_ADD_QUEUE); //声明的队列名
  }

  /**
   * 声明交换机
   * fanoutExchange:交换机类型,用的fanout类型.参数是交换机名称
   * durable(true) :交换机的持久化,true表示开启持久化
   * build() 构建出来交换机
   * @return
   */
  @Bean(GOODS_UP_EXCHANGE)
  public Exchange GOODS_UP_EXCHANGE() {
    return ExchangeBuilder.fanoutExchange(GOODS_UP_EXCHANGE).durable(true).build();
  }

  /**队列绑定交换机
   * @param queue   队列
   * @param exchange 交换机
   * @return
   */
  @Bean
  public Binding AD_UPDATE_QUEUE_BINDING(
          @Qualifier(SEARCH_ADD_QUEUE) Queue queue,
          @Qualifier(GOODS_UP_EXCHANGE) Exchange exchange
  ) {
    //把队列queue绑定到交换机exchange,  with()设置路由key
    return BindingBuilder.bind(queue).to(exchange).with("").noargs();
  }
}
